    <?php
    //some comment another comment'
    require_once('./alert_sent.class.php');
    
    $selectIdDtp = $_POST["selectIdDtp"];
    $text = $_POST["text"];
    $checked_alpha = $_POST["alpha"];
    $checked_kyivstar = $_POST["kyivstar"];
    $checked_email = $_POST["email"];
    $checked_motokiev = $_POST["motokiev"];
    
    $alert = new AlertSentClass();
    
    if($text != ""){  //получаем текст
            $data = $alert->getDBdata($selectIdDtp);            

            $subject = $alert->caseEvent($data['inf']['type']);

            $email_list = array();
            $kyivstar_list = array();
            $alpha_list = array();

            foreach ($data['recipients'] as $k => $v) {
                if($v['is_alert']){
                    array_push($email_list, $v['email']);
                }
                for($i=1;$i<5;$i++){
                    
                    $is_index = 'is_alert'.$i;
                    if($v[$is_index]){
                        $phone_index = 'phone'.$i;
                        $code = substr($v[$phone_index],0,3);
                        if($code == '039' || 
                            $code == '067' || 
                            $code == '068' ||
                            $code == '096' ||
                            $code == '097' ||
                            $code == '098' ){
                                array_push($kyivstar_list, $v[$phone_index]);
                            }
                            else{
                                array_push($alpha_list, $v[$phone_index]);
                            }
                    }
                }

            }
            
            $return['msg'] = '';
            if(count($email_list)>0 && $checked_email){
                if(isset($data['inf']['lat']) && isset($data['inf']['lng']))
                    $link = "http://31.131.16.130/yandex.php/script.php?lat=".$data['inf']['lat']."&lng=".$data['inf']['lng'];
                else
                    $link = '';
                $try = $alert->send_email($email_list, $text, $link);
                $return['msg'] .= $try['msg'];
            }
            if(count($kyivstar_list)>0 && $checked_kyivstar){
                $try = $alert->send_kyivstar($kyivstar_list, $text);
                $return['msg'] .= $try['msg'];
            }
            if(count($alpha_list)>0 && $checked_alpha){
                foreach($alpha_list as $number){
                    $try = $alert->send_alfa($number, $text);
                    $return['msg'] .= $try['msg'];
                }
            }
            if($checked_motokiev){

                $postfields = array(
                    "addpost" => "yes", 
                    "trid"=> "motohelp/1341940056928",
                    //"trid"=> "general/12638160105", 
                    "post_dt"=> "15", 
                    "email"=> "Amid.Kosh@gmail.com", 
                    "vp"=> "1024", 
                    "whoname"=> "", 
                    "message"=> iconv("UTF-8", "windows-1251", $text)
                    );                
                
                $url = 'http://moto.kiev.ua/forum/add.php';
                
                $header = array();
                $header[] = 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8';
                $header[] = 'Accept-Charset: Windows-1251,utf-8;q=0.7,*;q=0.7';
                $header[] = 'Accept-Language: ru-ru,ru;q=0.8,en-us;q=0.5,en;q=0.3';
                $header[] = 'Pragma: ';
                
                $ch = curl_init();    // initialize curl handle

                curl_setopt($ch, CURLOPT_URL, $url); //set target URL
                curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);// allow redirects
                curl_setopt($ch, CURLOPT_COOKIEFILE, './cookies.txt');
                curl_setopt($ch, CURLOPT_COOKIEJAR, './cookies.txt');
                curl_setopt($ch, CURLOPT_POST, TRUE); // set POST method
                curl_setopt($ch, CURLOPT_HTTPHEADER, $header); //set headers
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
                curl_setopt($ch, CURLOPT_HEADER, false); //return the headers so we can get session id
                curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields);
                $contents = curl_exec($ch);
                print_r();die($contents);
                curl_close($ch);
                if($contents) {
                    $try['msg'] = 'Message has been successfully sent to moto.kiev.ua';
                }else{
                    $try['msg'] = 'Failed sent to moto.kiev.ua';
                }
                $return['msg'] .= $try['msg'];
            }
    }else{
        $return['msg'] = "нет данных для отправки";
    }
    header('Content-type:  application/json.');
    echo json_encode($return);
    
?>